package com.mzy.linear_struct.hot100.回溯;

import java.util.ArrayList;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;

/**
 * @Author: mizy3
 * @Date: 2024/09/08/下午7:24
 * @Description:
 */
public class Pro77组合 {
    public List<List<Integer>> combine(int n, int k) {
        Deque<Integer> path = new LinkedList<>();
        List<List<Integer>> ans = new ArrayList<>();
        backtracking(n, k, 1, path, ans);
        return ans;
    }

    private void backtracking(int n, int k, int startIndex, Deque<Integer> path, List<List<Integer>> ans) {
        if(path.size() >= k){
            ans.add(new ArrayList<>(path));
            return;
        }

        for (int i = startIndex; i <= n; i++) {
            path.addLast(i);
            backtracking(n, k, i + 1, path, ans);
            path.removeLast();
        }
    }
}


























